package logmatcher.testgenerators;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.util.Scanner;

import javax.swing.JTextArea;

import org.apache.commons.collections.buffer.CircularFifoBuffer;

public class StreamRedirector implements Runnable{


	private Process process;
	private BufferedReader br;
	private JTextArea textArea;
	
	public StreamRedirector(Process p) {		
		process = p;
		textArea = BeanShellTester.getSippConsole();
		br = new BufferedReader(new InputStreamReader(p.getInputStream()));
		
		new Thread(this).start();
	}
	public StreamRedirector(InputStream is) {		

		textArea = BeanShellTester.getSippConsole();
		br = new BufferedReader(new InputStreamReader(is));
		
		new Thread(this).start();
	}
	public StreamRedirector(Reader reader) {		

		textArea = BeanShellTester.getSippConsole();
		br = new BufferedReader(reader);
		
		new Thread(this).start();
	}
	
	public void run() {	   
	   try {
		   
		   while(true){
			   String line = br.readLine();
			   if(line!=null){
				if(line.contains("Change Screen"))
					textArea.setText("");
				else 			
			        textArea.append(line+"\n");
				
				if (line.contains("Successful call")){
					Scanner scanner = new Scanner(line);
					scanner.findInLine("Successful call");
					String s = scanner.next();
					int success = scanner.nextInt();
					textArea.append("================== result is "+success+" ======================");
					System.out.println(">>> ====================== "+success+" ======================");
				}
					
			   }
		 
			  
		   }
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}		
	}
	
}
